package com.example.disruptor.demo.disruptor;

import com.lmax.disruptor.dsl.Disruptor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class LongEventPublisher implements Runnable{

    private CountDownLatch countDownLatch; // 用于监听初始化操作，等初始化执行完毕后，通知主线程继续工作
    private Disruptor<LongEvent> disruptor;
    private static volatile Integer NUM = 10; // 1,10,100,1000

    public LongEventPublisher(CountDownLatch countDownLatch, Disruptor<LongEvent> disruptor) {
        this.countDownLatch = countDownLatch;
        this.disruptor = disruptor;
    }

    @Override
    public void run() {
        LongEventTranslator2 eventTranslator = new LongEventTranslator2();
        try {
            for(int i = 0; i < NUM; i ++) {
                disruptor.publishEvent(eventTranslator);
                Thread.sleep(1000); // 假设一秒钟进一个数据
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown(); // 执行完毕后通知 await()方法
            log.info(NUM + "数据已经全部进入进入！");
        }
    }

}
